<?php

namespace app\modules\Product\controllers;

use Yii;
use app\modules\Product\models\ProductLog;
use app\modules\Product\models\ProductLogSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * 上传报价单信息
 */
class LogController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

   
    public function actionIndex()
    {
        $searchModel = new ProductLogSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

   
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

   
    public function actionCreate()
    {
        $model = new ProductLog();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

   
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    
    //下载报价单
    public function actionDownload($id){

        $model = $this->findModel($id);
        $name = $model->name;
        $url = $model->file_path;
        
        $url = iconv("utf-8","gb2312",$url); 
        if(file_exists($url)){
            header("Content-type: application/octet-stream");    
            header("Accept-Ranges: bytes");    
            header("Accept-Length: " . filesize($url));    
            header("Content-Disposition: attachment; filename=" .$name);    
            @readfile($url);
        }else{
            return false;
        } 

    }      

    //删除文件
    public function actionDelFile($id){

        $model = $this->findModel($id);

        if($model){
            if($model->user_id != \yii::$app->user->id){
                print_r(json_encode(['code'=>0,'message'=>'没有权限删除!']));
                return false;
            }
            $model->status = 2;
            if($model->save()){
                print_r(json_encode(['code'=>1,'message'=>'删除成功!']));
                return false;
            }
        }
        print_r(json_encode(['code'=>0,'message'=>'文件不存在!']));
        
    }
   

    
    protected function findModel($id)
    {
        if (($model = ProductLog::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
